#ifndef __M_UTILITY_HPP__
#define __M_UTILITY_HPP__

#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <fstream>
#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>
#include "logger.hpp"

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

//MySQL工具类
class mysql_util
{
public:
    //完成MYSQL前期准备工作
    static MYSQL* mysql_create(const std::string& host,const std::string& user,const std::string& password,
        const std::string& db,uint16_t port=3306)
    {
        //1.初始化
        MYSQL* mysql=mysql_init(nullptr);
        if(mysql==nullptr)
        {
            ELOG("mysql init failed!!!");
            return nullptr;
        }
        //2.连接数据库 
        if(mysql_real_connect(mysql,host.c_str(),user.c_str(),password.c_str(),db.c_str(),port,nullptr,0)==nullptr)
        {
            ELOG("mysql connect failed:%s",mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        //3.设置字符集
        if(mysql_set_character_set(mysql,"utf8")!=0)
        {
            ELOG("character set failed:%s",mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }

    //SQL语句的执行
    static bool mysql_exec(MYSQL* mysql,const std::string& query)
    {
        int res=mysql_query(mysql,query.c_str());
        if(res!=0)
        {
            ELOG("%s",query.c_str());
            ELOG("mysql query failed:%s",mysql_error(mysql));
            return false;
        }
        return true;
    }

    //关闭MYSQL句柄
    static bool mysql_destroy(MYSQL* mysql)
    {
        if(mysql!=nullptr)
        {
            mysql_close(mysql);
            return true;
        }
        return false;
    }
};

//Json工具类
class json_util
{
public:
    //Json序列化
    static bool serialize(const Json::Value& root,std::string& out)
    {
        //定义一个Json工厂类
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        int res=sw->write(root,&ss);
        if(res!=0)
        {
            ELOG("Json serialize failed!!!");
            return false;
        }
        out=ss.str();
        return true;
    }

    //Json反序列化
    static bool unserialize(const std::string& str,Json::Value& out)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        bool res=cr->parse(str.c_str(),str.c_str()+str.size(),&out,&err);
        if(res==false)
        {
            ELOG("Json unserilize failed:%s",err.c_str());
            return false;
        }
        return true;
    }
};

class string_util
{
public:
    static size_t split(const std::string& str,const std::string& sep,std::vector<std::string>& out)
    {
        size_t pos=0;
        size_t start=0;
        size_t size=str.size();
        while(start<size)
        {
            pos=str.find(sep,start);
            if(pos==std::string::npos)
            {
                out.push_back(str.substr(start));
                break;
            }
            //连续分隔符的情况
            if(pos==start)
            {
                start+=sep.size();
                continue;
            }
            out.push_back(str.substr(start,pos-start));
            start=pos;
            start+=sep.size();
        }
        return out.size();
    }
};

class file_util
{
public:
    static bool read(const std::string& filename,std::string& content)
    {
        //打开文件
        std::ifstream in(filename,std::ios::binary);
        if(in.is_open()==false)
        {
            ELOG("%s file open failed!!!",filename.c_str());
            return false;
        }
        //获取文件内容长度
        in.seekg(0,in.end);
        size_t len=in.tellg();
        in.seekg(0,in.beg);
        //读取文件内容
        content.resize(len);
        in.read(&content[0],len);
        if(in.good()==false)
        {
            ELOG("read %s file failed!!!",filename.c_str());
            in.close();
            return false;
        }
        //关闭文件
        in.close();
        return true;
    }
};

#endif